//
//  MapController.m
//  TelethonMobile
//
//  Created by Etienne Rugeri on 03/12/11.
//  Copyright (c) 2011 Air France. All rights reserved.
//

#import "MapController.h"
#import "Lieu.h"
#import "Annotation.h"
#import "Notation.h"

@interface MapController ()

@end

@implementation MapController

@synthesize mapView;
@synthesize locationToDisplay;


- (id)initWithLocationArray:(NSArray *)locations
{
    self = [super init];
    if (self) {
        self.locationToDisplay = locations;
    }
    return self;
}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
}

#pragma mark - View lifecycle

- (void)viewDidLoad
{
    [super viewDidLoad];
    // Do any additional setup after loading the view from its nib.
    
    self.mapView.showsUserLocation = YES;    
    
    CLLocationCoordinate2D location = CLLocationCoordinate2DMake(48.8531475f, 2.3721549f);
    
    MKCoordinateRegion region = MKCoordinateRegionMakeWithDistance(location, 10000.0, 10000.0);
    [self.mapView setRegion:region animated:YES];
    
    NSMutableArray *annotationsToAdd = [NSMutableArray array];

    NSLog(@"%i",[self.locationToDisplay count]);
    
    for (Lieu *lieu in self.locationToDisplay) {
        Annotation *annotation = [[Annotation alloc] init];
        annotation.lieu = lieu;
        [annotationsToAdd addObject:annotation];
        [annotation release];
    }
    
    [mapView addAnnotations:annotationsToAdd];
}

- (void)viewDidUnload
{
    [super viewDidUnload];
    // Release any retained subviews of the main view.
    // e.g. self.myOutlet = nil;
}

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
    // Return YES for supported orientations
    return (interfaceOrientation == UIInterfaceOrientationPortrait);
}

- (void)dealloc {
    self.mapView = nil;
    self.locationToDisplay = nil;
    [super dealloc];
}

#pragma mark - mapView delegate

//- (MKAnnotationView *)mapView:(MKMapView *)sender viewForAnnotation:(id <MKAnnotation>)annotation
//{
//	Annotation *a = (Annotation *) annotation;
//    NSString *id = [NSString stringWithFormat:@"pin_%i_%i", a.station.reco != NO_RECO, a.station.ficheName != nil];
//    
//    MKAnnotationView *aView = [sender dequeueReusableAnnotationViewWithIdentifier:id];
//    if (aView == nil) {
//        aView = [[[MKAnnotationView alloc] initWithAnnotation:annotation reuseIdentifier:id] autorelease];
//    } else {
//        aView.annotation = annotation;
//    }
//    
//    return aView;
//}

- (void)mapView:(MKMapView *)mapView didAddAnnotationViews:(NSArray *)annotationViews
{
//    for (MKAnnotationView *annView in annotationViews)
//    {
//        annView.alpha = 0;
//        [UIView animateWithDuration:0.75 animations:^{ annView.alpha = 1; }];
//    }
}

- (void)mapView:(MKMapView *)sender didSelectAnnotationView:(MKAnnotationView *)aView
{
//    [self.searchBar resignFirstResponder];
//    [mapView deselectAnnotation:aView.annotation animated:YES];
//    
//    DisplayDetailViewController *displayDetailViewcontroller = [[DisplayDetailViewController alloc] initWithNibName:@"DisplayDetailViewController" bundle:nil];
//    
//    displayDetailViewcontroller.annotation = (Annotation *)aView.annotation;
//    displayDetailViewcontroller.mainView = self;
//    
//    [self clearDetail];
//    
//    currentDetailPopOver = [[UIPopoverController alloc] initWithContentViewController:displayDetailViewcontroller];
//    [currentDetailPopOver setPopoverContentSize:displayDetailViewcontroller.view.frame.size];  
//    [currentDetailPopOver presentPopoverFromRect:aView.frame inView:aView.superview permittedArrowDirections:UIPopoverArrowDirectionAny animated:YES];  
//    
//    currentDetailPopOver.passthroughViews = [NSArray arrayWithObject:self.mapView];
}

- (void)mapView:(MKMapView *)mapView regionWillChangeAnimated:(BOOL)animated
{

}

- (void)mapView:(MKMapView *)mView regionDidChangeAnimated:(BOOL)animated
{

}

- (MKAnnotationView *) mapView: (MKMapView *) inMapView viewForAnnotation: (id<MKAnnotation>) annotation {
    if (annotation == mapView.userLocation) { 
        return nil; 
    } 
    
    MKPinAnnotationView *pin = (MKPinAnnotationView *) [inMapView dequeueReusableAnnotationViewWithIdentifier: @"annotation_ID"];
    if (pin == nil) {
        pin = [[[MKPinAnnotationView alloc] initWithAnnotation: annotation reuseIdentifier: @"annotation_ID"] autorelease];
    } else {
        pin.annotation = annotation;
    }
    
    if (self.locationToDisplay.count < 30) {
        pin.animatesDrop = YES;        
    } else {
        pin.animatesDrop = NO;
    }

    pin.canShowCallout = YES;
    
    UIButton *button = [UIButton buttonWithType:UIButtonTypeDetailDisclosure];
    [button addTarget:self action:@selector(show:) forControlEvents:UIControlEventTouchUpInside];

    
    //NSLog(@"%i", [self.locationToDisplay indexOfObject:((Annotation *)annotation).lieu]);
    button.tag = [self.locationToDisplay indexOfObject:((Annotation *)annotation).lieu];
    
    pin.rightCalloutAccessoryView = button;

    return pin;
}

-(void)show:(UIButton *)button
{
    Lieu *lieu = [self.locationToDisplay objectAtIndex:button.tag];
    
    Notation *notation = [[[Notation alloc] initWithLieu:lieu] autorelease];
    
    UINavigationController *nc = [[UINavigationController alloc] initWithRootViewController:notation];
    
    [notation setModalTransitionStyle:UIModalTransitionStyleFlipHorizontal];
    [self presentModalViewController:nc animated:YES];
}

@end
